<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>添加学生信息</title>
    <div th:replace="common/admin_commonElement :: css"></div>
    <style>
        .tree li {
            list-style-type: none;
            cursor: pointer;
        }

        .tree-closed {
            height: 40px;
        }

        .tree-expanded {
            height: auto;
        }
    </style>
</head>

<body>
<div th:replace="common/admin_commonElement :: nav"></div>
<div class="container-fluid">
    <div class="row">
        <div th:replace="common/admin_commonElement :: sider"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <ol class="breadcrumb">
                <li>功能面板</li>
                <li>学生管理</li>
                <li class="active">添加学生信息</li>
            </ol>
            <div class="panel panel-default">
                <div class="panel-heading">学生信息
                    <div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i
                            class="glyphicon glyphicon-question-sign"></i></div>
                </div>
                <div class="panel-body">
                    <form method="post">
                        <input type="hidden" name="_method" value="PUT">
                        <input type="hidden" name="sid" value="${student.sid}">
                        <div class="form-group">
                            <label for="id">学号</label>
                            <input type="text" class="form-control" id="id" name="id"
                                   placeholder="请输入学号">
                        </div>
                        <div class="form-group">
                            <label for="name">姓名</label>
                            <input type="text" class="form-control" id="name" name="name"
                                   placeholder="请输入学生姓名">
                        </div>
                        <div class="form-group">
                            <label>所在班级</label>
                            <br>
                            <select class="form-control" id="cid" name="cid">
                                <option th:each="grade : ${grades}" th:value="${grade.id}">[[${grade.name}]]</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="password">初始密码</label>
                            <input type="text" class="form-control" id="password" name="password"
                                   placeholder="请输入初始密码">
                        </div>
                        <input id="addBtn" type="button" class="btn btn-success glyphicon glyphicon-plus"
                               value="添加学生信息">
                    </form>
                </div>
            </div>
        </div>
    </div>

</div>
<div th:replace="common/admin_commonElement :: jsFile"></div>
<script type="text/javascript">
    layui.use('layer', function () {
        var layer = layui.layer;
    });

    $(function () {
        $(".list-group-item").click(function () {
            if ($(this).find("ul")) {
                $(this).toggleClass("tree-closed");
                if ($(this).hasClass("tree-closed")) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });
    });

    $("#addBtn").click(function () {
        var id = $("#id").val();
        var name = $("#name").val();
        var cid = $("#cid").val();
        var password = $("#password").val();

        console.log(id);
        console.log(name);
        console.log(cid);
        console.log(password);

        $.ajax({
            //url
            url: projectPath + '/admin/addStudent',
            //参数:
            data: {
                id: id,
                name: name,
                "grade.id": cid,
                password: password
            },
            //请求类型：
            type: 'post',
            //响应体结果：
            dataType: 'json',
            //成功回调：
            success: function (data) {
                if (data.result == "SUCCESS") {
                    layer.open({
                        title: "添加成功"
                        , content: "修改成功，3秒后自动跳转到学生列表！请耐心等待……"
                        , time: 3000
                        , end: function () {
                            window.location.href = projectPath + "/admin/toStudentList";
                        }
                    });
                } else {
                    layer.open({
                        title: "添加失败"
                        , content: data.message
                        , time: 3000
                        , end: function () {
                            window.location.href = projectPath + "/admin/toAddStudent";
                        }
                    });
                }
            },
            //失败回调：
            error: function () {
                layer.open({
                    title: "添加失败"
                    , content: "添加失败，可能是服务器故障，请稍后再试……"
                    , time: 3000
                    , end: function () {
                        window.location.href = projectPath + "/admin/toAddStudent";
                    }
                });
            },
        });
    });

</script>
</body>
</html>